<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="sendReceivables">创建</el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<el-form-item label="负责人" style="padding-top: 20px;">
					<select-user-dialog v-model:accountId="search.accountId" :mode="false"/>
				</el-form-item>
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="标题 / 摘要"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="receivablesId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="合同名称" prop="title" width="400" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="客户名称" prop="customerName" width="500" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="收款状态" prop="received " width="100">
					<template #default="scope">
						<el-tag type="danger" effect="dark" v-if="scope.row.received=='0'">未收</el-tag>
						<el-tag type="warning" effect="dark" v-if="((scope.row.received>'0') && (scope.row.received<scope.row.unReceived))">未收清</el-tag>
						<el-tag type="success" effect="dark" v-if="scope.row.received==scope.row.unReceived">已收清</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="应收金额" prop="unReceived" width="150">
					<template #default="scope">
						{{ scope.row.unReceived }}/元
					</template>
				</el-table-column>
				<el-table-column label="应收日期" prop="receivablesTime" width="100"></el-table-column>
				<el-table-column label="已收金额" prop="received" width="100">
					<template #default="scope">
						{{ scope.row.received }}/元
					</template>
				</el-table-column>
				<el-table-column label="收应负责人" prop="userName" width="100"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="210">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="setReceivables(scope.row, scope.$index)">收款</el-button>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<receivables v-if="dialog.receivablesSave" ref="saveReceivablesDialog" @success="handleSaveSuccess" @closed="dialog.receivablesSave=false"></receivables>
	<receivablesdetails v-if="dialog.detailsSave" ref="saveDetailsDialog" @closed="dialog.detailsSave=false"></receivablesdetails>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import scSelectFilter from '@/components/scSelectFilter'
import receivablesdetails from './receivablesdetails'
import receivables from './receivables'
import saveDialog from './save'

export default {
	name: 'Contract Receivables Manage',
	components: {
		saveDialog,
		scSelectFilter,
		selectUserDialog,
		receivables,
		receivablesdetails
	},
	data() {
		return {
			dialog: {
				save: false,
				details:false,
				receivablesSave:false
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "收款状态",
					key: "status",
					multiple: false,
					options: [{
						value: '',
						label: '全部'
					}, {
						value: '1',
						label: '未收'
					}, {
						value: '2',
						label: '未收清'
					}, {
						value: '3',
						label: '已收清'
					}],
				}
			],
			apiObj: this.$API.finance.contract.getContractReceivablesList,
			selection: [],
			search: {
				date: '',
				accountId: '',
				status: '',
				keyword: null
			}
		}
	},
	mounted() {
	},
	methods: {
		//添加
		sendReceivables() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.saveDetailsDialog.open().setData(row)
			})
		},
		//删除
		async table_del(row) {
			var reqData = {receivablesId: row.receivablesId}
			var res = await this.$API.finance.contract.deleteContractReceivables.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var receivablesIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					receivablesIdArr.push(this.selection[i].receivablesId);
				}
				const loading = this.$loading();
				var res = await this.$API.finance.contract.deleteContractReceivablesByIds.post({ids: receivablesIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		setReceivables(row, index) {
			this.dialog.receivablesSave = true
			this.$nextTick(() => {
				this.$refs.saveReceivablesDialog.open().setData({receivablesId:row.receivablesId})
			})
		},
		filterChange(data) {
			this.search.date = data.date;
			this.search.newsType = data.newsType;
			this.search.status = data.status;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
